#include "SdkMediaCommon.h"
#include "SdkVideo.h"

#define DEF_TAG             "MediaCom"

Com_u8 g_Sdk_MediaCom_Video_SleepMode = DEF_COM_FALSE;

SdkMediaComSetAudio g_Sdk_MediaCom_SetMic_CB = NULL;
SdkMediaComResetAudio g_Sdk_MediaCom_ResetMic_CB = NULL;
SdkMediaComSetAudio g_Sdk_MediaCom_SetSpk_CB = NULL;
SdkMediaComResetAudio g_Sdk_MediaCom_ResetSpk_CB = NULL;

Com_void Ext_SdkMediaCom_SetSleepMode(Com_u8 val)
{
    g_Sdk_MediaCom_Video_SleepMode = val;
}

Com_u8 Ext_SdkMediaCom_GetSleepMode(Com_void)
{
    return g_Sdk_MediaCom_Video_SleepMode;
}

Com_void Ext_SdkMediaCom_SetMicCB(Com_u8 v_sta,Com_s32 v_val,Com_u8 g_sta,Com_s32 g_val)
{
    if(g_Sdk_MediaCom_SetMic_CB != NULL)
    {
        g_Sdk_MediaCom_SetMic_CB(v_sta,v_val,g_sta,g_val);
    }
}

Com_void Ext_SdkMediaCom_SetMicCBRegister(SdkMediaComSetAudio func)
{
    g_Sdk_MediaCom_SetMic_CB = func;
}

Com_void Ext_SdkMediaCom_ResetMicCB(Com_void)
{
    if(g_Sdk_MediaCom_ResetMic_CB != NULL)
    {
        g_Sdk_MediaCom_ResetMic_CB();
    }
}

Com_void Ext_SdkMediaCom_ResetMicCBRegister(SdkMediaComResetAudio func)
{
    g_Sdk_MediaCom_ResetMic_CB = func;
}

Com_void Ext_SdkMediaCom_SetSpkCB(Com_u8 v_sta,Com_s32 v_val,Com_u8 g_sta,Com_s32 g_val)
{
    if(g_Sdk_MediaCom_SetSpk_CB != NULL)
    {
        g_Sdk_MediaCom_SetSpk_CB(v_sta,v_val,g_sta,g_val);
    }
}

Com_void Ext_SdkMediaCom_SetSpkCBRegister(SdkMediaComSetAudio func)
{
    g_Sdk_MediaCom_SetSpk_CB = func;
}

Com_void Ext_SdkMediaCom_ResetSpkCB(Com_void)
{
    if(g_Sdk_MediaCom_ResetSpk_CB != NULL)
    {
        g_Sdk_MediaCom_ResetSpk_CB();
    }
}

Com_void Ext_SdkMediaCom_ResetSpkCBRegister(SdkMediaComResetAudio func)
{
    g_Sdk_MediaCom_ResetSpk_CB = func;
}

Com_void Ext_SdkMediaCom_SetIRcutAndIRled(Com_s32 mode)
{
    if(mode == NIGHT_MODE_ON)
    {
        Ext_SdkGpioWrite_IRCut(IRCUT_NIGHT_MODE);
        usleep(100 * 1000);
        Ext_SdkGpioWrite_IRLed(DEF_COM_LEVEL_HIGHT);
    }
    else if (mode == NIGHT_MODE_OFF)
    {
        Ext_SdkGpioWrite_IRLed(DEF_COM_LEVEL_LOW);
        usleep(100 * 1000);
        Ext_SdkGpioWrite_IRCut(IRCUT_DAY_MODE);
    }
}

Com_void Ext_SdkMediaCom_GetMDSnapshot(_OUT Com_ps8 snap_addr,_OUT Com_ps32 snap_size)
{
    char snapfile[128];

    memset(snapfile,0,sizeof(snapfile));

    *snap_size = 0;
    sprintf(snapfile,"%s/%s",DEF_COM_SAVE_JEP_PATH,DEF_COM_JPEG_NAME);
    FILE *fp = fopen(snapfile,"rb");
    if(NULL == fp)
    {
        PRT_ERR(DEF_TAG,"fail to open %s\n",DEF_COM_JPEG_NAME);
        return;
    }

    fseek(fp,0,SEEK_END);
    *snap_size = ftell(fp);
    PRT_DBG(DEF_TAG,"get one snapshot,size: %d\n",*snap_size);
    if((*snap_size) >= DEF_SNAP_PIC_MAX_SIZE)
    {
        *snap_size = 0;
        fclose(fp);
        Ext_InComFile_Remove(snapfile);
        return;
    }

    fseek(fp,0,SEEK_SET);
    fread(snap_addr,*snap_size,1,fp);
    fclose(fp);

    Ext_InComFile_Remove(snapfile);
}
